<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";

export default {
  name: 'Scroll',
  props: {
    //probeType默认0，1时不滚动，2是不惯性滚动，3是惯性滚动
    probeType: {
      type: Number,
      default: 1,
    },
    pullUpLoad: {
      type: Boolean,
      default: false,
    },
    observeDom: {
      type: Boolean,
      default: true,
    },
    observeImage: {
      type: Boolean,
      default: true,
    },
    data: () => {
      return []
    }
  },
  data() {
    return {
      scroll: null,
    }
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.wrapper,{
      click: true,
      mouseWheel: true,
      observeDOM: this.observeDom,
      observeImage: this.observeImage,
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad,
    })
    //监听滚动的位置
    if(this.probeType == 2 || this.probeType == 3) {
      this.scroll.on('scroll', (position) => {
        this.$emit('scroll',position);
      })
    }
    //监听下拉加载
    if(this.pullUpLoad) {
      this.scroll.on('pullingUp', () => {
        this.$emit('pullingUp')
      })
    }
  },
  methods: {
    scrollTo(x,y,time = 300) {
      //逻辑语言&&先判断  前边的值是否为falus
      this.scroll && this.scroll.scrollTo(x, y, time)
    },
    refresh() {
      this.scroll && this.scroll.refresh()
    },
    finishPullUp() {
      this.scroll && this.scroll.finishPullUp()
    },
    getSaveY() {
      return this.scroll ? this.scroll.y : 0
      //如果this.scroll是undefin返回0 否则返回this.scroll.y
    }
  },

}
</script>

<style scoped>

</style>